package com.ebo.kline.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ebo.commonlib.utils.DigitUtils;
import com.ebo.kline.R;
import com.ebo.kline.model.IKData;
import com.ebo.kline.util.KUtils;

import org.jetbrains.annotations.Nullable;

import java.util.Locale;

/**
 * Created by dell on 2017/9/25.
 */

public class KLineChartInfoView extends ChartInfoView {

    private TextView mTvOpenPrice;
    private TextView mTvClosePrice;
    private TextView mTvHighPrice;
    private TextView mTvLowPrice;
    private TextView mTvChangeRate;
    private TextView mTvVol;
    private TextView mTvTime;
    private TextView mUpdown;
    private View mVgChangeRate;

    private int increasingColor = Color.WHITE;
    private int decreasingColor = Color.WHITE;
    private int netrualColor = Color.WHITE;

    public KLineChartInfoView(Context context) {
        this(context, null);

    }

    public KLineChartInfoView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public KLineChartInfoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.view_kline_chart_info, this);
        mTvTime = (TextView) findViewById(R.id.tv_time);
        mTvOpenPrice = (TextView) findViewById(R.id.tv_open_price);
        mTvClosePrice = (TextView) findViewById(R.id.tv_close_price);
        mTvHighPrice = (TextView) findViewById(R.id.tv_high_price);
        mTvLowPrice = (TextView) findViewById(R.id.tv_low_price);
        mUpdown = (TextView) findViewById(R.id.tv_updown);

        mTvChangeRate = (TextView) findViewById(R.id.tv_change_rate);
        mTvVol = (TextView) findViewById(R.id.tv_vol);
        mVgChangeRate = findViewById(R.id.vg_change_rate);
        init(context);
    }

    private void init(Context context){
        decreasingColor = context.getResources().getColor(R.color.decreasing_color);
        increasingColor = context.getResources().getColor(R.color.increasing_color);
        netrualColor = context.getResources().getColor(R.color.netrual_color);
    }

    @Override
    public void setData(double lastClose, IKData data) {
//        mTvTime.setText(KUtils.formatData(data.getDate()));
        mTvTime.setText(data.getTime());
        mTvClosePrice.setText(KUtils.formatDecimal(data.getClose_price()));
        mTvOpenPrice.setText(KUtils.formatDecimal(data.getOpen_price()));
        mTvHighPrice.setText(KUtils.formatDecimal(data.getHigh()));
        mTvLowPrice.setText(KUtils.formatDecimal(data.getLow()));
        double updown = data.getClose_price() - data.getOpen_price();
        mUpdown.setText(KUtils.formatDecimal(updown));

        if (lastClose == 0) {
            mVgChangeRate.setVisibility(GONE);
        } else {
            mVgChangeRate.setVisibility(VISIBLE);
            mTvChangeRate.setText(String.format(Locale.getDefault(), "%.2f%%", (data.getClose_price() - data.getOpen_price()) / data.getOpen_price() * 100));
        }
        if(updown > 0){
            mUpdown.setTextColor(increasingColor);
            mTvChangeRate.setTextColor(increasingColor);
            mUpdown.setText("+"+mUpdown.getText().toString());
            mTvChangeRate.setText("+"+mTvChangeRate.getText().toString());
        }else if(updown < 0){
            mUpdown.setTextColor(decreasingColor);
            mTvChangeRate.setTextColor(decreasingColor);
        }else{
            mUpdown.setTextColor(netrualColor);
            mTvChangeRate.setTextColor(netrualColor);
        }


        mTvVol.setText(DigitUtils.doublestr(data.getVolume(),2));
        removeCallbacks(mRunnable);
        postDelayed(mRunnable, 2000);
    }

}
